package com.example.demos.service.impl;

import com.example.demos.dto.*;
import com.example.demos.entity.Goods;
import com.example.demos.entity.Store;
import com.example.demos.mapper.StoreMapper;
import com.example.demos.service.StoreService;
import com.example.demos.util.CheckQueryParams;
import com.example.demos.util.PageBean;
import com.example.demos.util.SendMsg;
import com.example.demos.vo.ChangeQueryListVO;
import com.example.demos.vo.StoreAlertVO;
import com.example.demos.vo.StoreManagerVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreMapper storeMapper;

    @Override
    public int add(Store store) {
        List<String> list = storeMapper.selectStoreName();
        boolean flag = list.stream().anyMatch(e -> e.equals(store.getName()));
        if (flag) {
            return 0;
        }
        storeMapper.add(store);
        return 1;
    }

    @Override
    public PageBean<List<Store>> select(Integer page, Integer rows, String name, BigInteger empUuid) {
        Page<Store> storeList = PageHelper.startPage(page, rows);
        storeMapper.select(name, empUuid);
        PageBean<List<Store>> listPageBean = new PageBean<>(storeList.getTotal(), storeList.getResult());
        return listPageBean;
    }

    @Override
    public Store selectone(Integer id) {
        return storeMapper.selectone(id);
    }

    @Override
    public int update(Store store) {
        List<String> list = storeMapper.selectStoreNameList(store.getUuid());
        // 修改仓库名 不能和其他仓库名相同 和本身相同不会报错
        for (int j = 0; j < list.size(); j++) {
            boolean equals = list.get(j).equals(store.getName());
            if (equals) {
                return 0;
            }
        }
        storeMapper.update(store);
        return 1;
    }

    @Override
    public int delete(Integer id) {
        int i = storeMapper.selectStoreIsDelete(id);
        if (i == 0){
            return storeMapper.delete(id);
        }else {
            return 0;
        }

    }

    @Override
    public PageBean<List<Store>> getStoreList() {
        List<Store> storeDetailList = storeMapper.getStoreList();
        return new PageBean<>(storeDetailList.size(), storeDetailList);
    }

    @Override
    public PageBean<List<Goods>> getGoodsList() {
        List<Goods> goodsList = storeMapper.getGoodsList();
        return new PageBean<>(goodsList.size(), goodsList);
    }

    @Override
    public PageBean<List<StoreDetailDTO>> getStoreDetailList(StoreDetailQueryDTO storeDetailQueryDTO) {
        try (Page<StoreDetailDTO> page = PageHelper.startPage(storeDetailQueryDTO.getPage(), storeDetailQueryDTO.getRows())) {
            List<StoreDetailDTO> storeDetailList = storeMapper.getStoreDetailList(storeDetailQueryDTO);
            return new PageBean<>(page.getTotal(), page.getResult());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public PageBean<List<ChangeRecordDTO>> getStoreChangeRecords(ChangeListQueryDTO changeListQueryDTO) {
        try (Page<ChangeRecordDTO> page = PageHelper.startPage(changeListQueryDTO.getPage(), changeListQueryDTO.getRows())) {
            List<ChangeRecordDTO> changeRecordsList = storeMapper.getChangeRecordsList(changeListQueryDTO);
            return new PageBean<>(page.getTotal(), page.getResult());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean addInventory(InventoryAddParamsDTO inventoryAddParamsDTO, HttpSession httpSession) {
        try {
            if (CheckQueryParams.checkEmpty(inventoryAddParamsDTO,5)
                    || inventoryAddParamsDTO.getNum() < 0){
                return false;
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        inventoryAddParamsDTO.setCreateTime(LocalDateTime.now());
        inventoryAddParamsDTO.setCreater((String) httpSession.getAttribute("username"));
        inventoryAddParamsDTO.setState("未审核/编辑");
        return storeMapper.addInventory(inventoryAddParamsDTO) == 1;
    }

    @Override
    public PageBean<List<ChangeQueryListVO>> getInventoryListByPage(InventoryQueryDTO inventoryQueryDTO) {
        System.out.println("测试输出："+inventoryQueryDTO);
        try {
            InventoryQueryDTO queryDTO =(InventoryQueryDTO) CheckQueryParams.filterSpace(inventoryQueryDTO);
            try (Page<ChangeQueryListVO> page = PageHelper.startPage(queryDTO.getPage(), queryDTO.getRows())) {
                List<ChangeQueryListVO> list = storeMapper.getInventoryList(queryDTO);
                return new PageBean<>(page.getTotal(), page.getResult());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public ChangeQueryListVO getInventoryById(Integer id) {
        return storeMapper.getInventoryById(id);
    }

    @Override
    public int editInventoryById(InventoryEditParamsDTO editParamsDTO, HttpSession httpSession) {
        editParamsDTO.setChecker(httpSession.getAttribute("username").toString());
        editParamsDTO.setCheckTime(LocalDateTime.now());
        editParamsDTO.setState("已审核/编辑");
        return storeMapper.editInventoryById(editParamsDTO);
    }

    @Override
    public int deleteInventoryById(Integer id) {
        return storeMapper.deleteInventoryById(id);
    }

    @Override
    public PageBean<List<StoreAlertVO>> getStoreAlertInfo(Integer pageNum, Integer rows) {
        try (Page<StoreAlertVO> page = PageHelper.startPage(pageNum, rows)) {
            storeMapper.getStoreAlertInfo();
            return new PageBean<>(page.getTotal(), page.getResult());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public boolean sendMsg(String tel) {
        try {
            return SendMsg.sendMessage(tel);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<StoreManagerVo> getStoreManager() {
        return storeMapper.getStoreManager();
    }


}
